//
//  ReportCategoryViewController.m
//  HRJiWei
//
//  Created by zsj1992 on 16/12/12.
//  Copyright © 2016年 bjhj. All rights reserved.
//

#import "ReportCategoryViewController.h"
#import "HJCategory.h"
#import "CategoryCell.h"

@interface ReportCategoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ReportCategoryViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.hidesBackButton = YES;
    [self.navigationController setNavigationBarHidden:YES];

    
    
//    [self loadData];
    
}


-(NSMutableArray *)catogories{

    if (_catogories==nil) {
        _catogories = [NSMutableArray array];
    }
    return _catogories;
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.catogories.count;
}


-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CategoryCell * cell = [CategoryCell cellWithTableView:tableView];
    
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    
    HJCategory * category = self.catogories[indexPath.row];
    
    cell.category = category;
    
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ReportCategoryViewController * categoryVc = [[ReportCategoryViewController alloc]init];
    
    HJCategory * category = self.catogories[indexPath.row];
    
    [self loadData:category];
    

}

-(void)loadData:(HJCategory *)category{
    
    
    NSString * ID = category.ID;
    
    NSMutableArray * arrTemp = [NSMutableArray array];
    
    NSMutableDictionary * parameter = [NSMutableDictionary dictionary];
   
    [parameter setObject:ID  forKey:@"parentId"];

    [NetWork GET:getReportCategory parmater:parameter Block:^(NSData *data) {
        NSArray * arrDic = (NSArray *)data;
        for (NSDictionary * dic in arrDic) {
            HJCategory * category = [HJCategory mj_objectWithKeyValues:dic];
            [arrTemp addObject:category];
        }
        
        if (arrTemp.count>0) {
            NSLog(@"跳转");
            ReportCategoryViewController * categoryVc = [[ReportCategoryViewController alloc]init];
            categoryVc.catogories = arrTemp;
            [self.navigationController pushViewController:categoryVc animated:YES];
        }else{
//            [self.navigationController popViewControllerAnimated:YES];
//            [self.navigationController popToRootViewControllerAnimated:YES];
            
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
            
            NSDictionary *  userInfo = @{@"category":category};
            
            NSNotification * notification = [[NSNotification alloc]initWithName:@"selectCategory" object:nil userInfo:userInfo];
            
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            
        }
    }];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
